package com.yhp.service.impl;

import com.yhp.bean.Menu;
import com.yhp.bean.Users;
import com.yhp.dao.impl.UsersDaoImpl;
import com.yhp.service.UsersService;

import java.util.ArrayList;
import java.util.List;

public class UsersServiceImpl implements UsersService {
    UsersDaoImpl usersDao = new UsersDaoImpl();
    @Override
    public Users login(String name, String pass) {
        Users login = null;
        try {
            login = usersDao.login(name, pass);
            List<Menu> success = new ArrayList<>();
            List<Menu> list = usersDao.menuList(login.getRoleId());
            for (Menu menu : list) {
                if(menu.getUpMenuId()==0){
                    List<Menu> sc = new ArrayList<>();
                    for (Menu menu1 : list) {
                        if(menu1.getUpMenuId()==menu.getMenuId()){
                            sc.add(menu1);
                        }
                    }
                    menu.setSuccessList(sc);
                    success.add(menu);
                }
            }
            login.setMenuList(success);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return login;
    }

    @Override
    public List<Users> GetUsers(int pageIndex, int pageSize) {
        return usersDao.GetUsers(pageIndex, pageSize);
    }

    @Override
    public int total() {
        return usersDao.total();
    }

    @Override
    public int addUser(Users user) {
        return usersDao.addUser(user);
    }

    @Override
    public Users getId(int id) {
        return usersDao.getId(id);
    }

    @Override
    public int upDate(Users users) {
        return usersDao.upDate(users);
    }

    @Override
    public int delete(int id) {
        return usersDao.delete(id);
    }
}
